package com.slmxz.tms.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slmxz.tms.biz.mapper.OrderCargoMapper;
import com.slmxz.tms.biz.model.entity.OrderCargo;
import com.slmxz.tms.biz.service.OrderCargoService;
import com.slmxz.tms.common.component.PrimaryKeyGeneratorService;
import com.slmxz.tms.common.component.RedisUserService;
import com.slmxz.tms.common.enums.TableEnum;
import com.slmxz.tms.system.model.vo.UserInfoVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author：albert
 * @Date：2025/3/31 20:13
 */
@Service
public class OrderCargoServiceImpl extends ServiceImpl<OrderCargoMapper, OrderCargo> implements OrderCargoService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void adjustOrderCargo(List<OrderCargo> orderCargos,String orderNo) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        UserInfoVO user = RedisUserService.currentUser();
        //1.获取当前订单在数据库里的货物数据
        QueryWrapper<OrderCargo> orderCargoQuery = new QueryWrapper<>();
        orderCargoQuery.lambda().eq(OrderCargo::getOrderNo,orderNo);
        List<OrderCargo> orginalOrderCargos = this.list(orderCargoQuery);
        //2.如果只有数据库中不存在订单号对应的货物,代表全部是新增
        if(CollectionUtils.isEmpty(orginalOrderCargos)){
            for(OrderCargo orderCargoItem : orderCargos){
                orderCargoItem.setId(PrimaryKeyGeneratorService.generateId(TableEnum.TABLE_ORDER_CARGO));
                orderCargoItem.setOrderNo(orderNo);
                orderCargoItem.setUpdateBy(user.getId());
                orderCargoItem.setUpdateTime(now);
            }
            this.saveBatch(orderCargos);
            return;
        }
        //3.从原来的订单货物信息里面找出新增的数据
        List<OrderCargo> addOrderCargos = orderCargos.stream().filter(item -> item.getId() == null).toList();
        if(CollectionUtils.isNotEmpty(addOrderCargos)){
            for(OrderCargo orderCargoItem : addOrderCargos){
                orderCargoItem.setId(PrimaryKeyGeneratorService.generateId(TableEnum.TABLE_ORDER_CARGO));
                orderCargoItem.setOrderNo(orderNo);
                orderCargoItem.setUpdateBy(user.getId());
                orderCargoItem.setUpdateTime(now);
            }
            this.saveBatch(addOrderCargos);
        }
        //4.从原来的订单货物信息里面找出修改的数据
        List<OrderCargo> updateOrderCargos = new ArrayList<>();
        List<Long>  orginalOrderCargoIds = orginalOrderCargos.stream().map(OrderCargo::getId).toList();
        for (OrderCargo orderCargoItem : orderCargos){
            if(orderCargoItem.getId() == null){
                //排除需要新增的数据
                continue;
            }
            if(orginalOrderCargoIds.contains(orderCargoItem.getId())){
                orderCargoItem.setUpdateBy(user.getId());
                orderCargoItem.setUpdateTime(now);
                updateOrderCargos.add(orderCargoItem);
            }
        }
        if(CollectionUtils.isNotEmpty(updateOrderCargos)){
            //修改发生变更的数据
            this.updateBatchById(updateOrderCargos);
        }
        //5.找到需要删除的数据
        List<Long> orderCargoIds = orderCargos.stream().map(OrderCargo::getId).filter(Objects::nonNull).toList();
        List<Long> deleteOrderCargoIds  = orginalOrderCargos.stream().map(OrderCargo::getId).filter(id -> !orderCargoIds.contains(id)).toList();
        if(CollectionUtils.isNotEmpty(deleteOrderCargoIds)){
            //删除前端已经去掉的数据
            this.removeByIds(deleteOrderCargoIds);
        }
    }
}
